package com.zzyl.nursing.mapper;

import com.zzyl.nursing.dto.AppointmentDto;
import com.zzyl.nursing.vo.AppointmentVo;
import com.zzyl.nursing.vo.CountByTimeVo;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface MemberOrderMapper {

    @Select("select count(*) from reservation where  status= 2 and update_by=#{userId} and update_time between #{startTime} and #{endTime}")
    int getCancelledCountByUserId(@Param("userId") Long userId,@Param("startTime") LocalDateTime startTime,
                                      @Param("endTime") LocalDateTime endTime);

    //2.查询每个时间段剩余预约次数
    @Select("select time, 6 - COUNT(*) as count from reservation" +
            " where `time` between #{startTime} and #{endTime}" +
            " and status != 2 group by `time`")
    List<CountByTimeVo> getCountByTime( @Param("startTime") LocalDateTime startTime,@Param("endTime") LocalDateTime endTime);


    //3.新增预约
    @Insert("insert into reservation (name,mobile,time,visitor,type,status,create_time,create_by,update_time,update_by) values (#{name},#{mobile},#{time},#{visitor},#{type},#{status},now(),#{userId},now(),#{userId})")
    void addReservation(AppointmentDto appointmentDto);

    // 查询预约列表 - 支持根据status条件查询
    @Select({
            "<script>",
            "SELECT id, name, mobile, time, visitor, type, status, create_by as createBy, create_time as createTime, update_by as updateBy, update_time as updateTime ",
            "FROM reservation ",
            "<where>",
            "  <if test='status != null'>",
            "    status = #{status}",
            "  </if>",
            "</where>",
            "ORDER BY create_time DESC ",
            "LIMIT #{limit} OFFSET #{offset}",
            "</script>"
    })
    List<AppointmentVo> selectAppointmentList(@Param("offset") int offset, @Param("limit") int limit, @Param("status") Integer status);

    // 查询预约总数 - 支持根据status条件统计
    @Select({
            "<script>",
            "SELECT COUNT(*) FROM reservation ",
            "<where>",
            "  <if test='status != null'>",
            "    status = #{status}",
            "  </if>",
            "</where>",
            "</script>"
    })
    int selectAppointmentCount(@Param("status") Integer status);

    //取消预约
    @Update(" UPDATE reservation \n" +
            "     SET status = 2, \n" +
            "         update_time = now(), \n" +
            "         update_by = #{userId} \n" +
            "     WHERE id = #{id}")
    void deleteAppointmentById(@Param("id") Long id, @Param("userId") Long userId);

    //根据id查询预约
    @Select("SELECT id, name, mobile, time, visitor, type, status, create_by as createBy, create_time as createTime, update_by as updateBy, update_time as updateTime " +
            "FROM reservation " +
            "WHERE create_by = #{userId}")
    List<AppointmentVo> selectAppointmentById(Long userId);

    //更新预约状态
    @Update("UPDATE reservation SET status = #{status}, update_time = now(), update_by = #{userId} WHERE id = #{id}")
    void updateAppointmentStatus(AppointmentVo appointment);
}